package com.test.latitudex.utilities;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.latitudex.R;

public class MyListAdapter extends BaseAdapter {
	private Activity activity;
	private static LayoutInflater inflater = null;
	public static final String KEY_NAME = "displayName";
	public static final String KEY_ID = "id";
	public static final String KEY_DATE = "time";
	public static final String KEY_LONGITUDE = "longitude";
	public static final String KEY_LATITUDE = "latitude";
	public static final String KEY_ACCURACY = "accuracy";
	public static final String KEY_IMAGE = "avatarVersion";
	public static final String KEY_SELF = "self";
	private static final String LOG_TAG = "FriendActivity";
	
	public MyListAdapter(Activity a) {
		activity = a;
		inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return RegistrationManager.getEntity().getFriendData().size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public void removeDataFromPos(int position){
		RegistrationManager.getEntity().getFriendData().remove(position);
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		if (convertView == null)
			vi = inflater.inflate(R.layout.single_row, null);

		TextView name = (TextView) vi.findViewById(R.id.name); // title
		TextView location = (TextView) vi.findViewById(R.id.location); // artist name
		TextView lastUpdated = (TextView) vi.findViewById(R.id.lastUpdated); // duration
		ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image); // thumb image

		HashMap<String, String> friend = new HashMap<String, String>();
		friend = RegistrationManager.getEntity().getFriendData().get(position);

		// Setting all values in listview
		name.setText(friend.get(KEY_NAME));
		Location locationA = new Location("point A");
		Location locationB = new Location("point B");

		if(RegistrationManager.getEntity().getSelfData().get(KEY_LATITUDE) == null){
			location.setText("No data from current position.");
		}else if(friend.get(KEY_LATITUDE) == null){
			location.setText("No data from friend's position.");
		}else{
			locationA.setLatitude(Double.parseDouble(friend.get(KEY_LATITUDE)));
			locationA.setLongitude(Double.parseDouble(friend.get(KEY_LONGITUDE)));
			locationB.setLatitude(Double.parseDouble(RegistrationManager.getEntity().getSelfData().get(KEY_LATITUDE)));
			locationB.setLongitude(Double.parseDouble(RegistrationManager.getEntity().getSelfData().get(KEY_LONGITUDE)));

			float distance = locationA.distanceTo(locationB);
			DecimalFormat df = new DecimalFormat("#0.00");
			if(distance >= 1000){
				location.setText(df.format((distance/1000)) + " kilometers far");
			}else{
				location.setText(df.format(distance) + " meters far");
			}
		}
		
				
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm", Locale.getDefault());
		try {
			if(friend.get(KEY_DATE) == null){
				lastUpdated.setText("No data uploaded yet.");
			}else{
				Date date = format.parse(friend.get(KEY_DATE));
				long diff = new Date().getTime() - date.getTime();
				long seconds = diff / 1000;
				long minutes = seconds / 60;
				long hours = minutes / 60;
				long days = hours / 24;
				String diffStr = "";
				if (days > 0) {
					diffStr = String.valueOf(days) + " days and "
							+ String.valueOf((diff - (1000 * 60 * 60 * 24 * days)) / (1000 * 60 * 60)) + " hours ago";
				} else if (hours > 0) {
					diffStr = String.valueOf(hours) + " hours and "
							+ String.valueOf((diff - (1000 * 60 * 60 * hours)) / (1000 * 60)) + " minutes ago";
				} else if (minutes > 0) {
					diffStr = String.valueOf(minutes) + " minutes and "
							+ String.valueOf((diff - (1000 * 60 * minutes)) / 1000) + " seconds ago";
				} else {
					diffStr = String.valueOf(seconds) + " seconds ago";
				}
				lastUpdated.setText(diffStr);
			}
			

		} catch (ParseException e) {
			Log.e(LOG_TAG, e.getMessage());
		}
		Drawable d = activity.getResources().getDrawable(R.drawable.thumb);

		thumb_image.setImageDrawable(d);
		return vi;
	}
}